package com.wlr.blog.article.controller;

import com.wlr.blog.util.common.aspect.LogTrack;
import com.wlr.blog.util.common.dto.Result;
import com.wlr.blog.util.common.strategy.SearchContext;
import com.wlr.blog.web.common.entity.Article;
import com.wlr.blog.web.common.service.ArticleService;
import com.wlr.blog.web.common.utils.PageResult;
import com.wlr.blog.web.common.vo.ArchivesVO;
import com.wlr.blog.web.common.vo.ArticleSearchVO;
import com.wlr.blog.web.common.vo.ArticleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/11/8 21:10
 */
@RestController
@Slf4j
@Api(value = "文章接口类")
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @Autowired
    SearchContext<ArticleSearchVO> searchArticleSearchContext;

    @ApiOperation(value = "获取文章列表")
    @GetMapping("/list")
    @LogTrack
    public Result<?> getArticleList(Article article) {
        PageResult pageResult = articleService.queryPage(article);
        return Result.success(pageResult);
    }

    @ApiOperation(value = "根据编号获取文章")
    @GetMapping("/{id}")
    @LogTrack
    public Result<ArticleVo> getArticleId(@PathVariable Integer id) {
        ArticleVo article = (ArticleVo) articleService.getById(id);
        return Result.success(article);
    }

    @ApiOperation(value = "保存文章内容")
    @ApiParam(name = "mapper/article", value = "文章信息，保存必传")
    @PostMapping("/save")
    @LogTrack
    public Result save(@RequestBody Article article) {
        boolean save = articleService.save(article);
        return save ? Result.success() : Result.error("保存失败");
    }

    @ApiOperation(value = "搜索文章内容")
    @ApiParam(name = "keywords", value = "关键词")
    @GetMapping("/search")
    @LogTrack
    public Result<List<ArticleSearchVO>> searchArticle(@RequestParam("keywords") String keywords) {
        List<ArticleSearchVO> articleSearchVOS = searchArticleSearchContext.execSearch(keywords);
        return Result.success(articleSearchVOS);
    }

    @ApiOperation(value = "查询归档节点")
    @ApiParam(name = "params", value = "分页参数")
    @GetMapping("/archives")
    @LogTrack
    public Result<?> getArchivesList(ArchivesVO params) {
        return Result.success(articleService.getArchivesList(params));
    }
}
